//
//  ZYReturnRedbagVCCell.swift
//  HengYouCai
//
//  Created by lwj on 2018/6/14.
//  Copyright © 2018年 lwj. All rights reserved.
//

import UIKit




class ZYReturnRedbagVCCell: UITableViewCell {

   /// 赋值
    override func wl_configurationCell(_ modelTemp: Any?, _ listView: UIScrollView, _ indexPath: IndexPath) {
        if let dataArr = modelTemp as? [RedPackModel]{
            let model = dataArr[indexPath.row]
            
            var strendtime = model.endtime
            strendtime = strendtime?.replacingOccurrences(of: "\n", with: "：")
            self.dateLab.text = strendtime
            
            if model.status == "-1" || model.status == "1"{ // 已过期
                self.cv.image = UIImage(named: "cash_overdue")
            }else{ // 可使用
                self.cv.image = UIImage(named: "cash_bonus")
            }
            
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = 3
            
            //  处理富文本编辑框 余额 & 面额
            let text = NSMutableAttributedString(string: model.sources ?? "", attributes: [NSForegroundColorAttributeName : ColorFromRGBHex_WL(0xffffff),NSFontAttributeName:Font_WL(13.aw_wl)])
            
            text.append(NSAttributedString(string: "￥", attributes: [NSForegroundColorAttributeName:ColorFromRGBHex_WL(0xffffff),NSFontAttributeName:Font_WL(13.aw_wl)]))
            text.append(NSAttributedString(string: model.bonuses, attributes: [NSForegroundColorAttributeName:ColorFromRGBHex_WL(0xffffff),NSFontAttributeName:BoldFont_WL(23.aw_wl)]))
            text.append(NSAttributedString(string: "\n", attributes: [NSForegroundColorAttributeName:ColorFromRGBHex_WL(0xffffff),NSFontAttributeName:Font_WL((12.aw_wl))]))
            text.append(NSAttributedString(string: model.conditionLabel, attributes: [NSForegroundColorAttributeName:ColorFromRGBHex_WL(0xffffff),NSFontAttributeName:Font_WL(12.aw_wl)]))
//            text.append(NSAttributedString(string: "\n", attributes: [NSForegroundColorAttributeName:ColorFromRGBHex_WL(0xffffff),NSFontAttributeName:Font_WL((12.aw_wl))]))
//            text.append(NSAttributedString(string: model.hbcondition, attributes: [NSForegroundColorAttributeName:ColorFromRGBHex_WL(0xffffff),NSFontAttributeName:Font_WL(12.aw_wl)]))
//            text.append(NSAttributedString(string: "，", attributes: [NSForegroundColorAttributeName:ColorFromRGBHex_WL(0xffffff),NSFontAttributeName:Font_WL((12.aw_wl))]))
//            text.append(NSAttributedString(string: model.hbcondition2, attributes: [NSForegroundColorAttributeName:ColorFromRGBHex_WL(0xffffff),NSFontAttributeName:Font_WL((12.aw_wl))]))

            text.addAttributes([NSParagraphStyleAttributeName:paragraphStyle], range: NSRange(location: 0, length: text.length))
            
            self.moneyLab.attributedText = text
        }
    }
    
    /// 加息券赋值
     func wl_CouponconfigurationCell(modelTemp: Any?, listView: UIScrollView, indexPath: IndexPath) {
        if let dataArr = modelTemp as? [RedPackModel]{
            let model = dataArr[indexPath.row]
            
            var strendtime = model.endtime
            strendtime = strendtime?.replacingOccurrences(of: "\n", with: "：")
            self.dateLab.text = strendtime
            
            if model.status == "-1" || model.status == "1" { // 已过期
                self.cv.image = UIImage(named: "interest_overdue")
            }else { // 可使用
                self.cv.image = UIImage(named: "interest_bonus")
            }
            
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = 3
            
            //  处理富文本编辑框 新手奖励
            let text = NSMutableAttributedString(string: model.sources ?? "", attributes: [NSForegroundColorAttributeName : ColorFromRGBHex_WL(0xffffff),NSFontAttributeName:Font_WL(13.aw_wl)])
            
            let condition = "\n" +  model.conditionLabel //model.hbcondition + "，" + model.hbcondition2
            
            text.append(NSAttributedString(string: model.bonuses, attributes: [NSForegroundColorAttributeName:ColorFromRGBHex_WL(0xffffff),NSFontAttributeName:BoldFont_WL(23.aw_wl)]))
            
            text.append(NSAttributedString(string: condition, attributes: [NSForegroundColorAttributeName:ColorFromRGBHex_WL(0xffffff),NSFontAttributeName:Font_WL((12.aw_wl))]))
        
            
            
            text.addAttributes([NSParagraphStyleAttributeName:paragraphStyle], range: NSRange(location: 0, length: text.length))
            
            self.moneyLab.attributedText = text
        }
    }
    ///加息券赋值
    public func wl_touZiCouponConfigurationCell(modelTemp: Any?, listView: UIScrollView, indexPath: IndexPath) {
        guard let dataArr = modelTemp as? [RedPackModel] else {
            return
        }
        let model = dataArr[indexPath.row]
        self.isUserInteractionEnabled = model.isAvailable
        
        self.dateLab.text = model.endtime
        
        var isValid = false
        if model.isAvailable{ // 可选择
            self.cv.image = UIImage(named: "interest_unbonus")
            isValid = true
            
        }else{ // 不可使用
            self.cv.image = UIImage(named: "interest_runout")
            
        }
        
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 2
        
        //  处理富文本编辑框 余额 & 面额
        let text = NSMutableAttributedString(string: model.bonuses, attributes: [NSForegroundColorAttributeName : isValid ? Color.white: Normal_666Color,NSFontAttributeName:Font_WL(23.aw_wl)])
        
              text.append(NSAttributedString(string: "\n" + model.conditionLabel, attributes: [NSForegroundColorAttributeName:Normal_666Color,NSFontAttributeName:Font_WL(12.aw_wl)]))
//        text.append(NSAttributedString(string: "\n" + model.hbcondition, attributes: [NSForegroundColorAttributeName:Normal_666Color,NSFontAttributeName:Font_WL(12.aw_wl)]))
//        text.append(NSAttributedString(string: "\n" + model.hbcondition2, attributes: [NSForegroundColorAttributeName:Normal_666Color,NSFontAttributeName:Font_WL((12.aw_wl))]))
        text.append(NSAttributedString(string: "\n" + model.sources, attributes: [NSForegroundColorAttributeName:Normal_666Color,NSFontAttributeName:Font_WL((12.aw_wl))]))
        
        text.addAttributes([NSParagraphStyleAttributeName:paragraphStyle], range: NSRange(location: 0, length: text.length))
        
        self.moneyLab.attributedText = text
        self.moneyLab.textColor = Color.white
        
    }
    
    /// 投资红包赋值
    func wl_touZiConfigurationCell(_ modelTemp: Any?, _ listView: UIScrollView, _ indexPath: IndexPath) {
        if let dataArr = modelTemp as? [RedPackModel]{
            
            
            let model = dataArr[indexPath.row]
            
            self.isUserInteractionEnabled = model.isAvailable
            //            var strendtime = model.endtime
            //            strendtime = strendtime?.replacingOccurrences(of: "\n", with: "：")
            self.dateLab.text = model.endtime
            
            var isValid = false
            if model.isAvailable{ // 可点击
                self.cv.image = UIImage(named: "cash_unbonus")
                isValid = true
                
            }else{ // 不可使用
                self.cv.image = UIImage(named: "cash_runout")
            }
            
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = 2
            
            //  处理富文本编辑框 余额 & 面额
            let text = NSMutableAttributedString(string: "￥", attributes: [NSForegroundColorAttributeName : isValid ? Color.white: Normal_666Color,NSFontAttributeName:Font_WL(13.aw_wl)])
            text.append(NSAttributedString(string: model.bonuses, attributes: [NSForegroundColorAttributeName:isValid ? Color.white: Normal_666Color,NSFontAttributeName:BoldFont_WL(23.aw_wl)]))
            text.append(NSAttributedString(string: "\n" + model.conditionLabel, attributes: [NSForegroundColorAttributeName:Normal_666Color,NSFontAttributeName:Font_WL(12.aw_wl)]))
//            text.append(NSAttributedString(string: "\n" + model.hbcondition2, attributes: [NSForegroundColorAttributeName:Normal_666Color,NSFontAttributeName:Font_WL((12.aw_wl))]))
//            text.append(NSAttributedString(string: "\n" + model.sources, attributes: [NSForegroundColorAttributeName:Normal_666Color,NSFontAttributeName:Font_WL((12.aw_wl))]))
            
            text.addAttributes([NSParagraphStyleAttributeName:paragraphStyle], range: NSRange(location: 0, length: text.length))
            
            self.moneyLab.attributedText = text
            self.moneyLab.textColor = Color.white
        }
    }
    static func wl_createCellWithTableView(_ tableView: UITableView) -> ZYReturnRedbagVCCell{
        let cellID = NSStringFromClass(ZYReturnRedbagVCCell.self)
        if let cell = tableView.dequeueReusableCell(withIdentifier: cellID) as? ZYReturnRedbagVCCell{
            return cell
        }else{
            return ZYReturnRedbagVCCell(style: .default, reuseIdentifier: cellID)
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?){
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.createContentView()
}

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
}

    private func createContentView(){
        self.backgroundColor = UIColor.clear
        self.contentView.addSubview(self.cv)
        self.cv.addSubview(self.moneyLab)
        self.cv.addSubview(self.dateLab)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        self.cv.frame = CGRect(x: 15,y: 0,width: self.contentView.width_sd - 30,height: self.contentView.height_sd - 7.5)
        
        self.moneyLab.bounds = CGRect(x: 0,y: 0, width: 275.aw_wl, height: self.cv.height)
        self.moneyLab.centerY_sd = self.cv.height_sd/2 - 20
        self.moneyLab.left_sd = 4/69*self.cv.width_sd
        
        self.dateLab.bounds = CGRect(x: 0, y: 0, width: 275.aw_wl, height: self.cv.height)
        self.dateLab.left_sd = 4/69*self.cv.width_sd
        self.dateLab.centerY_sd = self.cv.height_sd/2 + 27
      
}

    // 背景图
    lazy var cv: UIImageView = {
        let temp = UIImageView()
        temp.image = UIImage()
        return temp
    }()
    
    // 金额标签
    lazy var moneyLab: UILabel = {
        let temp = UILabel()
        temp.numberOfLines = 2
        return temp
    }()
    
    // 日期标签
    lazy var dateLab: UILabel = {
        let temp = UILabel()
        temp.textAlignment = .left
        temp.numberOfLines = 2
        temp.textColor = ColorFromRGBHex_WL(0xffffff)
        temp.font = Font_WL(12.aw_wl)
        return temp
    }()
  
}

























